/**
 * Created by wenchuan.li 2018/4/27.
 */
define(["public/app.min"],function () {

    var self, app = win.app, user;

    var reg = app.Module.extend({

        init: function () {
            self = this;

            self.data = {
                provinces: [],
                citys: [],
                industry: [],
                plants: [],
                customers: [],
                sub: {
                    GWAA017: '',//省
                    GWAA006: '1',
                    GWAA007: '1',
                    GWAA008: '',
                    GWAA018: '',
                    GWAK001: ''
                    //industry:'',
                }
            };
            self._super({
                _tag: 'register_p'
            }, win);

        },
        subData: function () {
            var _data = self.data.sub;
            if (!_data.GWAA002 || _data.GWAA002 == '') {
                app.toast('姓名不能为空');
                return;
            }
            if (!_data.GWAA005 || _data.GWAA005 == '') {
                app.toast('出生日期不能为空');
                return;
            }
            if (!_data.GWAA004 || _data.GWAA004 == '') {
                app.toast('手机号不能为空');
                return;
            }
            _data.GWAA001 = user.id;
            app.ajax({
                xurl: "api/wx/user/registerUser",
                loading: true,
                xdata: _data,
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '注册成功！',
                            ok: function () {
                                app.back(1);
                            }
                        });
                    } else {
                        app.alert({
                            msg: result.data.appmsg,
                            ok: function () {
                                app.back(1);
                            }
                        });
                    }
                }
            });
        },
        getProvince: function () {
            app.ajax({
                xurl: "api/wx/user/getProvinces",
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        var provinces = result.data.data;
                        self.data.sub.GWAA017 = provinces[0].GWAD002;
                        self.data.provinces.pushArray(provinces);

                        self.getCity();
                    }
                }
            });
        },
        getCity: function () {
            app.ajax({
                xurl: "api/wx/user/getCitys",
                xdata: {
                    GWAD003: self.data.sub.GWAA017
                },
                cb: function (result) {
                    if (result.ok) {
                        if (self.data.citys != 0) {
                            self.data.citys.splice(0, self.data.citys.length);
                        }
                        var citys = result.data.data;
                        self.data.sub.GWAA018 = citys[0].GWAD002;
                        self.data.citys.pushArray(citys);
                    }
                }
            });
        },
        getIndustry: function () {
            app.ajax({
                xurl: "api/wx/user/getGwbwDatas",
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        var industry = result.data.data;
                        self.data.sub.industry = industry[0].GWBW001;
                        self.data.industry.pushArray(industry);
                    }
                }
            });
        },
        getCustomers: function () {
            app.ajax({
                xurl: "api/wx/user/getGwakDatas",
                xdata: {
                    GWAK024: self.data.sub.GWAA018
                },
                cb: function (result) {
                    if (result.ok) {
                        if (self.data.customers.length != 0) {
                            self.data.customers.splice(0, self.data.customers.length)
                        }
                        var customers = result.data.data;
                        self.data.sub.GWAK001 = customers[0].GWAK001;
                        self.data.customers.pushArray(customers);
                    }
                }
            });
        },
        getPlant: function () {
            app.ajax({
                xurl: "api/wx/user/getPlantDatas",
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        var plants = result.data.data;
                        self.data.sub.GWAA008 = plants[0].ID;
                        self.data.plants.pushArray(plants);
                    }
                }
            });
        },
        _lazy: function () { // 延迟300ms加载

            self.getProvince();
            self.getIndustry();
            //self.getCustomers();

            //user = app.pageModule('p_main').userInfo;
            user = {
                id: app.cookie('_WX_ACCOUNT_'),
                name: app.cookie('_WX_USERNAME_'),
                mobile: app.cookie('_WX_MOBILE_')
            };
            self.data.sub.GWAA002 = user.name;
            self.data.sub.GWAA004 = user.mobile;
            app.taps.call(win, [{
                o: 'submitDiv',
                cb: function (e) {
                    self.subData();
                }
            }]);
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        },
        _ready: function () { // dom载入完成后执行
            new Vue({
                el: doc.querySelector('.mui-content'),
                data: self.data,
                methods: {
                    provinceChange: function () {
                        self.getCity();
                        self.getCustomers();
                    },
                    customersChange: function () {
                        self.getCustomers();
                    }
                }
            });
            self.getPlant();
        }
    });

    new reg();

});